<?php

namespace App\Admin\Controllers;

use App\Models\Express;
use App\Models\Area;

use Encore\Admin\Form;
use Encore\Admin\Grid;

use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

use Illuminate\Support\Facades\Request;


class ExpressController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('运费模板');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('运费模板');
            $content->description('管理');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('运费模板');
            $content->description('管理');

            $content->body($this->form());
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Express::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->title('配送名称');
            $grid->first('首重');
            $grid->continue('续重/kg');
            $grid->free_amount('代理额度');
            $grid->free_retail_amount('零售额度');
            $grid->area('区域')->display(function ($area) {
                $area = array_map(function ($area) {
                    return "<span class='label label-success'>{$area['name']}</span>";
                }, $area);
                if(!empty($area)){
                    $des = array_slice($area,0,10);
                    return join('&nbsp;', $des).'...';
                }else{
                    return join('&nbsp;', $area);
                }
            });
            //$grid->area('区域');
            $grid->disableExport();
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->filter(function ($filter) {
                $filter->useModal();
                $filter->like('title', '配送名称');
                $filter->between('created_at', '添加时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Express::class, function (Form $form) {
            $form->text('title', '配送名称')->rules('required');
            $form->number('first', '首重费用')->value(0);
            $form->number('continue', '续重费用/kg')->placeholder('每kg增加费用')->value(0);
            $form->number('free_amount', '代理免运费额度')->value(1100);
            $form->number('free_retail_amount', '零售免运费额度')->value(356);
            $form->multipleSelect('area', '区域')->options(Area::whereLevel(2)->get()->pluck('merger_name','id'));
        });
    }

}
